home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / baseClasses.c < prev    next >
C/C++ Source or Header  |  1996-02-15  |  4KB  |  203 lines

  1. /*
  2.  * baseClasses.c
  3.  * Handle base classes.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define    DBG(s)
  15.  
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include "gtypes.h"
  19. #include "classMethod.h"
  20. #include "baseClasses.h"
  21. #include "lookup.h"
  22. #include "md.h"
  23.  
  24. classes* StringClass;
  25. classes* ClassClass;
  26. classes* ObjectClass;
  27. strpair* initpair;
  28. strpair* finalpair;
  29. classes* classInitHead;
  30. int classInitLevel = 2;
  31.  
  32. #define    INIT        "<clinit>"
  33. #define    INITSIG        "()V"
  34. #define    FINAL        "finalize"
  35. #define    FINALSIG    "()V"
  36.  
  37. /*
  38.  * Initialise the machine.
  39.  */
  40. void
  41. initialise(void)
  42. {
  43.     /* Setup CLASSPATH */
  44.     initClasspath();
  45.  
  46.     /* Create the initialise launch pair */
  47.     initpair = addStringPair(INIT, INITSIG);
  48.  
  49.     /* Create the finalize launch pair */
  50.     finalpair = addStringPair(FINAL, FINALSIG);
  51.  
  52.     /* Read in base classes */
  53.     initBaseClasses();
  54.  
  55.     /* Setup exceptions */
  56.     initExceptions();
  57.  
  58.     /* Init native support */
  59.     initNative();
  60.  
  61.     /* Init thread support */
  62.     initThreads();
  63. }
  64.  
  65. /*
  66.  * We need to use certain classes in the internal machine so we better
  67.  * get them in now in a known way so we can refer back to them.
  68.  * Currently we need java/lang/Object, java/lang/Class and java/lang/String.
  69.  */
  70. void
  71. initBaseClasses(void)
  72. {
  73.     /* Start with the simple types. */
  74.     initTypes();
  75.  
  76.     /* Read in object */
  77.     ObjectClass = lookupClass(addString(OBJECTCLASS));
  78.     if (ObjectClass == 0) {
  79.         fprintf(stderr, "Failed to find class %s ... aborting.\n", OBJECTCLASS);
  80.         exit(1);
  81.     }
  82.  
  83.     /* Read in class */
  84.     ClassClass = lookupClass(addString(CLASSCLASS));
  85.     if (ClassClass == 0) {
  86.         fprintf(stderr, "Failed to find class %s ... aborting.\n", CLASSCLASS);
  87.         exit(1);
  88.     }
  89.  
  90.     /* Fixup mtable because it couldn't be made for the first classes */
  91.     ClassClass->head.type = ClassClass;
  92.     ClassClass->head.mtable = ClassClass->mtable;
  93.     ObjectClass->head.type = ClassClass;
  94.     ObjectClass->head.mtable = ClassClass->mtable;
  95.  
  96.     /* Read in strings */
  97.     StringClass = lookupClass(addString(STRINGCLASS));
  98.     if (StringClass == 0) {
  99.         fprintf(stderr, "Failed to find class %s ... aborting.\n", STRINGCLASS);
  100.         exit(1);
  101.     }
  102.  
  103.     initClasses();
  104. }
  105.  
  106. /*
  107.  * Creata a string in the string cache.
  108.  */
  109. char*
  110. addString(char* s)
  111. {
  112.     strconst* m;
  113.  
  114.     m = (strconst*)malloc(sizeof(strconst) + strlen(s) + 1);
  115.     strcpy(m->data, s);
  116.     return (addStringConstant(m));
  117. }
  118.  
  119. /*
  120.  * Create a string pair in the string pair cache.
  121.  */
  122. strpair*
  123. addStringPair(char* s1, char* s2)
  124. {
  125.     strpair* pair;
  126.  
  127.     pair = addStringConstantPair(addString(s1), addString(s2));
  128.     assert(pair != 0);
  129.  
  130.     return (pair);
  131. }
  132.  
  133. /*
  134.  * We translate a CONSTANT_Chararray to a CONSTANT_String on demand.
  135.  * The strings in the .class file are not held as real objects, so
  136.  * we convert them here.
  137.  */
  138. void
  139. makeStringObject(int idx, constants* pool)
  140. {
  141.     stringClass* obj;
  142.  
  143.     assert(pool->tags[idx] == CONSTANT_Chararray);
  144.  
  145.     obj = getString(STRING_DATA2BASE(pool->data[STRING_NAME(idx, pool)]));
  146.  
  147.     /* Install new object */
  148.     pool->tags[idx] = CONSTANT_String;
  149.     pool->data[idx] = (int)obj;
  150. }
  151.  
  152. /*
  153.  * Convert a strconst to a String object.
  154.  */
  155. stringClass*
  156. getString(strconst* str)
  157. {
  158.     stringClass* obj;
  159.  
  160.     if (str->string == 0) {
  161.         obj = (stringClass*)alloc_object(StringClass, true);
  162.         assert(obj != 0);
  163.  
  164.         obj->value = &str->obj;
  165.         obj->offset = 0;                /* ??? */
  166.         obj->count = obj->value->size;            /* ??? */
  167.         str->string = obj;
  168.     }
  169.     return (str->string);
  170. }
  171.  
  172. /*
  173.  * Initialise classes.
  174.  */
  175. void
  176. initClasses(void)
  177. {
  178.     classes* class;
  179.     classes* nclass;
  180.     void* func;
  181.  
  182.     classInitLevel++;
  183.  
  184.     class = classInitHead;
  185.     classInitHead = 0;
  186.     while (class != 0) {
  187.         class->state = CSTATE_OK;
  188.  
  189.         func = findMethod(class, initpair);
  190.         assert(func != 0);
  191. DBG(        printf("Initialising %s static %d\n", class->name, class->sfsize); fflush(stdout); )
  192.         CALL_KAFFE_FUNCTION_VARARGS(func, 0, 0, 0);
  193.         nclass = class->nextInit;
  194.         class->nextInit = 0;
  195.         class->prevInit = 0;
  196.         class = nclass;
  197.     }
  198.  
  199.     classInitLevel--;
  200.     /* We should never drop below our initial level of two */
  201.     assert(classInitLevel >= 2);
  202. }
  203.